package com.ts.api.module.product.service.impl;

import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.product.dao.ProductApplyFlowDao;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.qo.ProductApplyFlowQO;
import com.ts.api.module.product.service.ProductApplyFlowService;
import com.ts.api.task.log.ProductFlowApplyLogExecute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductApplyFlowServiceImpl implements ProductApplyFlowService {

    @Autowired
    private ProductApplyFlowDao productApplyFlowDao;
    @Autowired
    private ProductFlowApplyLogExecute productFlowApplyLogExecute;

    @Override
    @Transactional(readOnly = true)
    public List<ProductApplyFlowPO> list(ProductApplyFlowQO productApplyFlowQO) {
        return productApplyFlowDao.list(productApplyFlowQO);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ProductApplyFlowPO> listForDataExchange(ProductApplyFlowQO productApplyFlowQO) {
        return productApplyFlowDao.listForDataExchange(productApplyFlowQO);
    }

    @Override
    @Transactional(readOnly = true)
    public ProductApplyFlowPO get(ProductApplyFlowQO productApplyFlowQO) {
        return productApplyFlowDao.get(productApplyFlowQO);
    }

    @Override
    @Transactional(readOnly = true)
    public int countStatus(ProductApplyFlowQO productApplyFlowQO) {
        return productApplyFlowDao.countStatus(productApplyFlowQO);
    }

    @Override
    public void insert(ProductApplyFlowPO productApplyFlowPO, boolean async) {
        if (ValidParam.isNull(productApplyFlowPO.getCreateTime())) {
            productApplyFlowPO.setCreateTime(DateUtils.currentTime());
        }

        //  开启异步插入
        if (async) {
            productFlowApplyLogExecute.execute(productApplyFlowPO);
            return ;
        }
        productApplyFlowDao.insert(productApplyFlowPO);
    }

    @Override
    public void batchInsert(List<ProductApplyFlowPO> listProductApplyFlowPO) {
        IteratorUtils.array(listProductApplyFlowPO, t -> t.setCreateTime(DateUtils.currentTime()));
        productApplyFlowDao.batchInsert(listProductApplyFlowPO);
    }

    @Override
    public int updateFlowStatusById(ProductApplyFlowPO productApplyFlowPO) {
        return productApplyFlowDao.updateFlowStatusById(productApplyFlowPO);
    }
}
